package cn.dida.service.impl;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.dida.dao.ActivityWinningDao;
import cn.dida.dao.CompanyDao;
import cn.dida.model.ActivityWinning;
import cn.dida.model.Company;
import cn.dida.model.DeliveryInfo;
import cn.dida.model.EntPayEntity;
import cn.dida.model.Pageable;
import cn.dida.model.RedPackEntity;
import cn.dida.service.ActivityWinningService;
import cn.dida.util.AliyunMqUtil;
/**
 * <p>兑奖记录   业务层接口实现类</p>
 * @author lisha
 * @date 2016-11-20 14:27
 * */
@Service
public class ActivityWinningServiceImpl implements ActivityWinningService {

	@Autowired
	private ActivityWinningDao winDao;
	
	
	@Override
	public ActivityWinning getWinningById(String winningId) {
		// TODO Auto-generated method stub
		return winDao.getWinningById(winningId);
	}
	@Transactional
	@Override
	public Integer updateActWinningCashSta(Integer actwinid, Integer prizeid,Integer issuccess) {
		return winDao.sendEnvelope(actwinid,prizeid,issuccess);
	}
	@Transactional
	@Override
	public Integer sendEnvelope( Integer winningId, Integer prizeId,Integer source,Integer companyId){
//		return winDao.sendEnvelope(winningId,prizeId);
		String preference = winDao.getCompanyPreference(companyId);
		String messge_id = "" ;
		Map<String, Object> prizemap = winDao.queryPrizeInfoMapById(prizeId, winningId);
		
		Double price = NumberUtils.toDouble(prizemap.get("price").toString(), 0.00);
		Double total_amount = price * 100;
		String act_name = prizemap.get("actname").toString();
		String openid =  prizemap.get("weixin_openid").toString();
		if(act_name.length()>10){
			act_name = act_name.substring(0, 10)+"...";
		}
		JSONObject json = JSONObject.fromObject(preference);
		if(source==0){
			RedPackEntity redpack=new RedPackEntity();
            redpack.setMchId(json.getString("weixin_mh_id"));
            redpack.setMchKey(json.getString("weixin_mh_pay_key"));
            redpack.setAppId(json.getString("weixin_gzh_appid"));
            redpack.setReOpenid(openid);
            redpack.setClientIp(ResourceBundle.getBundle("app").getString("runtime.ip"));
            redpack.setTotalAmount(total_amount.intValue());
            redpack.setActName(act_name);
            redpack.setSendName(json.getString("weixin_gzh_name"));
            redpack.setTotalNum(1);
            redpack.setRemark("抽奖现金红包");
            redpack.setWishing("恭喜发财");
            redpack.setCallBackUrl(ResourceBundle.getBundle("app").getString("aliyun.mq.callbackaddr")+"customerredbagscallback");
            messge_id = AliyunMqUtil.sendRedPackPayMessage(redpack);
		}else if(source==1) {
			EntPayEntity entPay=new EntPayEntity();
            entPay.setCallBackUrl(ResourceBundle.getBundle("app").getString("aliyun.mq.callbackaddr")+"customerredbagscallback");
            entPay.setAmount(total_amount.intValue());
            entPay.setAppId(json.getString("weixin_gzh_appid"));
            entPay.setDescription("企业红包");
            entPay.setMchId(json.getString("weixin_mh_id"));
            entPay.setOpenId(openid);
            entPay.setMchKey(json.getString("weixin_mh_pay_key"));
            messge_id = AliyunMqUtil.sendEntPayMessage(entPay);
		}
		//删除以前相同的记录
		winDao.deleteActivity_winning_sendred_log(winningId);
		//重新插入数据
	    winDao.insertActivity_winning_sendred_log(winningId, Calendar.getInstance().getTimeInMillis(),source,openid,total_amount,messge_id);
		
		return 1;
	}
	@Override
	public List<Map<String,Object>> getSendEvnFalse(Integer companyId,String activityId){
		return winDao.getSendEvnFalse(companyId,activityId);
	}
	
	@Override
	public String getCompanyApid(Integer companyId){
		String preference = winDao.getCompanyPreference(companyId);
		String weixin_gzh_appid ;
		if(preference != null){
			JSONObject json = JSONObject.fromObject(preference.toString());
			if (null != json && json.containsKey("weixin_gzh_appid")){
				weixin_gzh_appid=json.get("weixin_gzh_appid").toString();
			}else{
				weixin_gzh_appid= null;
			}
		}else{
			weixin_gzh_appid= null;
		}
		return weixin_gzh_appid;
	}

	@Override
	public Pageable<ActivityWinning> queryWinning(Integer compayId,String qrcodeNum, String qrcodeName,String productName,String prizeName,
												  String csshMobile,String cashTimeS,String cashTimeE,String scanTimeS,String scanTimeE,Integer page,Integer shipMethod
			,String activityId,Integer redEnvelope) {
		// TODO Auto-generated method stub
		return winDao.query(compayId,qrcodeNum, qrcodeName, productName, prizeName, csshMobile, cashTimeS, cashTimeE,
				scanTimeS, scanTimeE, page,shipMethod,activityId,redEnvelope);
	}

	@Override
	public DeliveryInfo getDeliveryInfo(String winningId) {
		// TODO Auto-generated method stub
		return winDao.getDeliveryInfo(winningId);
	}

	@Override
	public Pageable<ActivityWinning> queryWinningList(String activityId,
													  Integer companyId, String qrcodeNum, String qrcodeName,
													  String productName, String prizeName, String csshMobile,
													  String cashTimeS, String cashTimeE, String scanTimeS,
													  String scanTimeE, Integer page,Integer shipMethod) {
		// TODO Auto-generated method stub
		return winDao.getActivityWinningList(activityId, companyId, qrcodeNum, qrcodeName, productName,
				prizeName, csshMobile, cashTimeS, cashTimeE, scanTimeS, scanTimeE, page, shipMethod);
	}

	@Override
	public List<ActivityWinning> queryWinningForExport(Integer companyId,String activityId,
													   String qrcodeNum, String qrcodeName, String productName,
													   String prizeName, String csshMobile, String cashTimeS,
													   String cashTimeE, String scanTimeS, String scanTimeE, Integer shipMethod,Integer redEnvelope) {
		// TODO Auto-generated method stub
		return winDao.queryForExport(companyId,activityId, qrcodeNum, qrcodeName, productName, prizeName, csshMobile, cashTimeS,
				cashTimeE, scanTimeS, scanTimeE, shipMethod,redEnvelope);
	}

	@Override
	public  Map<String, Object>  queryMessage_Activity_winning_ID(String message_id) {
		// TODO Auto-generated method stub
		return winDao.queryMessage_Activity_winning_ID(message_id);
				
	}
	
	@Override
	public Integer calbackupdateActivitywinningsendredlog(String messageid, Integer issuccess, String err_code_des) {
		// TODO Auto-generated method stub

		return winDao.calbackupdateActivitywinningsendredlog(messageid, issuccess, err_code_des);
	}

	@Override
	public List<Map<String,Object>> statisticalFormList(Integer companyId,String activityId){

		List<Map<String,Object>> list =winDao.statisticalFormList(companyId,activityId);
		return list;
	}
	

}
